package cn.edu.xmu.restfuldemo.service;

import cn.edu.xmu.restfuldemo.dao.GoodsSkuDao;
import cn.edu.xmu.restfuldemo.dao.OrderItemDao;
import cn.edu.xmu.restfuldemo.dao.OrdersDao;
import cn.edu.xmu.restfuldemo.model.BO.GoodsSku;
import cn.edu.xmu.restfuldemo.model.BO.OrderItem;
import cn.edu.xmu.restfuldemo.model.BO.Orders;
import cn.edu.xmu.restfuldemo.model.VO.GoodsSkuRetVo;
import cn.edu.xmu.restfuldemo.model.VO.OrderWithItemsSkuRetVo;
import cn.edu.xmu.restfuldemo.model.VO.OrdersRetVo;
import cn.edu.xmu.restfuldemo.util.ReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Zijun Min
 * @description
 * @createTime 2021/10/22 17:45
 **/
@Service
public class GoodsSkuService {
    @Autowired
    private GoodsSkuDao goodsSkuDao;

    @Autowired
    OrdersDao ordersDao;

    @Autowired
    OrderItemDao orderItemDao;

    /**
     * dao
     * @param id
     * @return
     */
    public ReturnObject<OrderWithItemsSkuRetVo> findGoodsSkuDao(Long id){
        ReturnObject<Orders> returnObjectAboutOrders = ordersDao.findOrdersById(id);
        ReturnObject<List<OrderItem>> returnObjectAboutOrderItemList = orderItemDao.findOrderItemListByOrderId(id);
        List<GoodsSku>goodsSkus=new ArrayList<>();
        for(OrderItem orderItem:returnObjectAboutOrderItemList.getData()){
            GoodsSku goodsSku=new GoodsSku();
            goodsSku=goodsSkuDao.findGoodsSkuByOrderId(orderItem.getGoodsSkuId()).getData();
            goodsSkus.add(goodsSku);
        }
        return new ReturnObject<>(new OrderWithItemsSkuRetVo((Orders)returnObjectAboutOrders.getData(),(List<OrderItem>)returnObjectAboutOrderItemList.getData(),goodsSkus));
    }

    /**
     * sql
     * @param id
     * @return
     */
    public ReturnObject<OrderWithItemsSkuRetVo> findGoodsSkuSql(Long id){
        return goodsSkuDao.findGoodsSkuByOrderIdSql(id);
    }

    /**
     * 不加redis
     * @param id
     * @return
     */
    public ReturnObject<GoodsSkuRetVo> findGoodsSkuById(Long id){
        ReturnObject<GoodsSku> returnObject =  goodsSkuDao.findGoodsSkuByOrderId(id);
        return new ReturnObject<>(new GoodsSkuRetVo(returnObject.getData()));
    }

    /**
     * 加redis
     * @param id
     * @return
     */
    public ReturnObject<GoodsSkuRetVo> findGoodsSkuByIdUsingRedis(Long id){
        ReturnObject<GoodsSku> returnObject =  goodsSkuDao.findGoodsSkuByOrderIdUsingRedis(id);
        return new ReturnObject<>(new GoodsSkuRetVo(returnObject.getData()));
    }


}
